﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.DataModel;
using System.Data;

namespace QLBH.Model.Controller
{
    public class Kho_MathangController:QLBHEntityBase<KHO_MATHANG>
    {
        public static QLBHEntityBase<KHO_MATHANG> GetInstance()
        {
            return new Kho_MathangController();
        } 
        public override KHO_MATHANG Add(KHO_MATHANG entity)
        {
            try
            {
                KHO_MATHANG kmh = FindBy(k => k.MHID == entity.MHID && k.KHOID == entity.KHOID).FirstOrDefault();
                if (kmh != null)
                {
                    //exist in database, need to edit
                    kmh.SOLUONG = entity.SOLUONG;
                    kmh.GIABANBUON = entity.GIABANBUON;
                    kmh.GIABANLE = entity.GIABANLE;
                    kmh.GIANHAP = entity.GIANHAP;
                    kmh.CHIETKHAU = entity.CHIETKHAU;                    
                    this.Context.ObjectStateManager.ChangeObjectState(kmh, EntityState.Modified);
                }
                else
                {
                    // not exist in database, need to add
                    kmh = base.Add(entity);
                }
                Save();
                return kmh;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public override KHO_MATHANG Edit(KHO_MATHANG entity)
        {
            try
            {
                KHO_MATHANG kmh = FindBy(k => k.MHID == entity.MHID && k.KHOID == entity.KHOID).FirstOrDefault();
                if (kmh != null)
                {
                    //exist in database, need to edit
                    kmh.SOLUONG = entity.SOLUONG;
                    kmh.GIABANBUON = entity.GIABANBUON;
                    kmh.GIABANLE = entity.GIABANLE;
                    kmh.GIANHAP = entity.GIANHAP;
                    kmh.CHIETKHAU = entity.CHIETKHAU;
                    this.Context.ObjectStateManager.ChangeObjectState(kmh, EntityState.Modified);
                }
                else
                {
                    // not exist in database, need to add
                    kmh = base.Add(entity);
                }
                Save();
                return kmh;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }            
        }
    }
}
